$.extend({
    _popshow:function(id,placement,content,title)
    {
        $(id).attr('data-placement',placement);
        $(id).attr('data-content',content);
        $(id).attr('data-original-title',title);
        $(id).popover('show');
    },
    _is_account:function(account)
    {
        var _regx = /^[a-z0-9]*$/;
        if(account !='' && _regx.test(account))
            return true;
        else
            return false;
    },
    _is_email:function(email)
    {
        var _regx=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        if(email !='' && _regx.test(email))
            return true;
        else
            return false;
    },
    _show_error:function(say)
    {
       var  html ='<div id="alert-errors" class="modal hide fade">';
        html+='<div class="modal-header">';
        html+='<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';
        html+='<h3>消息提示</h3>';
        html+='</div>';
        html+='<div class="modal-body">';
        html+='<p>'+say+'</p>';
        html+='</div>';
        html+='<div class="modal-footer">';
        html+='<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>';
        html+='</div>';
        html+='</div>';
        $('body').append(html);
        $('#alert-errors').modal('toggle');
        return;
    },
    is_key:function(key)
    {
        if(key.length < 16 && key.length > 5)
            return true;
        else
            return false;
    },
    _sign_up:function()
    {
        var account = $("#account").val();
        var email = $("#email").val();
        var key = $("#key").val();
        if(!$._is_account(account))
        {
            $._popshow('#account','right','账户名称由英文(小写字母或数字组成).','提示!');
            return;
        }
        if(!$._is_email(email))
        {
            $._popshow('#email','right','邮箱不能为空或你的邮箱格式不正确.','提示!');
            return;
        }
        if(!$.is_key(key))
        {
            $._popshow('#key','right','密码不能为空,请填写(6~15个字符长度)的密码.','提示!');
            return;
        }
        $.post("/account/sign_up",
            {
                account: account,
                email: email,
                key: key,
            },
            function(data,status)
            {
                if(data.status)
                    location.href='/account/sign_in';
                else
                    $._show_error(data.msg);
            }
        );
    },

    _sign_in:function()
    {
        $.post("/account/sign_in",
            {
                email: $("#email").val(),
                key: $("#key").val()
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/";
                else
                    $._show_error(data.msg);
            }
        );
    },

    _new_site:function()
    {
        $.post("/site/new",
            {
                _type: 1,
                domain: $("#domain").val()
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/site/new?s="+data.domain+"|"+data.host
                else
                    $._show_error(data.msg);
            }
        );
    },

    _site_step:function()
    {
        $.post("/site/new",
            {
                _type: 2,
                name: $("#site_name").val(),
                domain: $("#domain").val(),
                host: $("#host").val(),
                level: $("#levels").val()
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/sites";
                else
                    $._show_error(data.msg);
            }
        );
    },
    _node_new:function()
    {
        $.post("/node/new",
            {
                _type: 'new',
                host: $("#host").val(),
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/node/new?h="+data.host;
                else
                    $._show_error(data.msg);
            }
        );
    },
    _node_token:function()
    {
        $.post("/node/new",
            {
                _type: 'token',
                name: $("#node_name").val(),
                level: $("#node_level").val(),
                host: $("#host").val(),
                token: $("#token").val(),
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/nodes";
                else
                    $._show_error(data.msg);
            }
        );
    },
    _node_show:function(_node)
    {
        $.post("/node/"+_node+"/status",
            {
                node: _node
            },
            function(data,status)
            {
                if(data.status)
                {
                    var _cpu_free = parseFloat(100-parseFloat(data.cpu_status));
                    var _men_free = parseFloat(100-parseFloat(data.memory_mem.per));
                    $("#system").html(data.system);
                    $("#cpu-us").css("width",data.cpu_status);
                    $("#cpu-free").css("width",_cpu_free+"%");
                    $("#men-us").css("width",data.memory_mem.per);
                    $("#men-free").css("width",_men_free+"%");
                    $("#ethx-all").html($._node_ethx_show(data.network));
                }
                else
                {
                    $._show_error(data.msg);
                }
            }
        );
    },
    _node_ethx_show:function(ethx)
    {
        var html ='';
        for(x in ethx)
        {
            html +='<tr>';
            html +='<td rowspan="2">';
            html +=x+'('+ethx[x].info.inet+')';
            html +='<td>流入</td>';
            html +='<td>'+ethx[x].count.receive_bytes+'</td>';
            html +='<td>'+ethx[x].count.receive_bytes_speed+'</td>';
            html +='</td>';
            html +='</tr>';
            html +='<tr>';
            html +='<td>流出</td>';
            html +='<td>'+ethx[x].count.transmit_bytes+'</td>';
            html +='<td>'+ethx[x].count.transmit_bytes_speed+'</td>';
            html +='</tr>'
        }
        return html;
    },
    _site_host_edit:function(host)
    {
        var html ='';
        html += '<input type="text" id="edit-host" value="'+host+'">';
        $('.site-host').html(html);
        $('#site-host-btn').attr('onclick','site_host_save()');
        $('#site-host-btn').html('保存');
    },
    _site_host_save:function(_site_id)
    {
        $.post("/site/"+_site_id+"/update",
            {
                _type: 'host',
                host: $('#edit-host').val()
            },
            function(data,status)
            {
                if(data.status)
                {
                    $('.site-host').html(data.host);
                    $('#site-host-btn').html('修改');
                    $('#site-host-btn').attr('onclick','site_host_edit("'+data.host+'")');
                }
                else
                {
                    $._show_error(data.msg);
                }
            }
        );
    },
    _site_domain_new:function()
    {
        var html='';
        html +='<div class="row"><div class="span3">'
        html +='<span>添加域名</span>'
        html +='<input type="text" id="add-domain">';
        html +='<a href="javascript:void(0)" class="btn btn-mini" onclick="site_domain_save(\'new\')">添加域名</a>';
        html +='</div></div>';
        $('.site-domain-add').html(html);
    },
    _site_domain_save:function(ac)
    {
        $.post("/site/"+_site_id+"/update",
            {
                _type: 'domain',
                _ac: ac,
                domain: $("#add-domain").val()
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/site/"+_site_id+'/edit';
                else
                    $._show_error(data.msg);
            }
        );
    },
    _site_domain_delete:function(_sid)
    {
        $.post("/site/"+_site_id+"/update",
            {
                _type: 'domain',
                _ac: 'delete',
                sid: _sid
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/site/"+_site_id+"/edit";
                else
                    $._show_error(data.msg);
            }
        );
    },
    _site_domain_edit:function(_sid)
    {
        $.post("/site/"+_site_id+"/update",
            {
                _type: 'domain',
                _ac: 'edit',
                sid: _sid
            },
            function(data,status)
            {
                if(data.status)
                {
                    var html='';
                    html +='<div class="row"><div class="span3">'
                    html +='<span>修改域名</span>'
                    html +='<input type="text" id="edit-domain" value="'+data.domain+'">';
                    html +='<a href="javascript:void(0)" class="btn btn-mini" onclick="site_domain_update('+data.id+')">编辑域名</a>';
                    html +='</div></div>';
                    $('.site-domain-add').html(html);
                }
                else
                    $._show_error(data.msg);
            }
        );
    },
    _site_domain_update:function(_sid)
    {
        $.post("/site/"+_site_id+"/update",
            {
                _type: 'domain',
                _ac: 'save',
                sid: _sid,
                domain: $("#edit-domain").val()
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/site/"+_site_id+"/edit";
                else
                    $._show_error(data.msg);
            }
        );
    },
    _site_port_edit:function(port)
    {
        var html ='';
        html += '<input type="text" class="span1" id="edit-port" value="'+port+'">';
        $('.site-port').html(html);
        $('#site-port-btn').attr('onclick','site_port_save()');
        $('#site-port-btn').html('保存');
    },
    _site_port_save:function()
    {
        $.post("/site/"+_site_id+'/update',
            {
                _type: "port",
                port: $("#edit-port").val()
            },
            function(data,status)
            {
                if(data.status)
                {
                    $('.site-port').html(data.port);
                    $('#site-port-btn').html('修改端口');
                    $('#site-port-btn').attr('onclick','site_port_edit("'+data.port+'")');
                }
                else
                    $._show_error(data.msg);
            }
        );
    },
    _new_level:function()
    {
        $.post("/node/level",
            {
                name: $("#level_name").val(),
                type: $("#level_type").val()
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/nodes";
                else
                    $._show_error(data.msg);
            }
        )
    },
    _save_level:function()
    {
        $.post("/level/"+_level_id+'/edit',
            {
                name: $("#level_name").val(),
                type: $("#level_type").val()
            },
            function(data,status)
            {
                if(data.status)
                    location.href="/node/levels";
                else
                    $._show_error(data.msg);
            }
        )
    },
    _site_clear_cache:function()
    {
        $.post("/site/"+_site_id+"/cache",
            {
                time: 'xxx'
            },
            function(data,status)
            {
                if(data.status)
                    $._show_error(data.say);
                else
                    $._show_error(data.msg);
            }
        )
    }
});
function sign_up()
{
    $._sign_up();
}
function sign_in()
{
    $._sign_in();
}
function new_site()
{
    $._new_site();
}
function site_step()
{
    $._site_step();
}
function new_node()
{
    $._node_new();
}
function token_node()
{
    $._node_token();
}
function node_show(_node)
{
    $._node_show(_node);
}
function site_host_edit(host)
{
    $._site_host_edit(host);
}
function site_host_save()
{
    $._site_host_save(_site_id);
}
function site_domain_new()
{
    $._site_domain_new();
}
function site_domain_save(ac)
{
    $._site_domain_save(ac);
}
function site_domain_delete(_sid)
{
    $._site_domain_delete(_sid);
}
function site_domain_edit(_sid)
{
    $._site_domain_edit(_sid);
}
function site_domain_update(_sid)
{
    $._site_domain_update(_sid);
}
function site_port_edit(port)
{
    $._site_port_edit(port);
}
function site_port_save()
{
    $._site_port_save();
}
function new_level()
{
    $._new_level();
}
function SaveLevel()
{
    $._save_level();
}
function site_clear_cache()
{
    $._site_clear_cache();
}
